package com.aiyige.page.publish.majorcourse;

import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentManager;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.aiyige.R;
import com.aiyige.page.publish.majorcourse.adapter.MajorCourseTypeAdapter;
import com.aiyige.page.publish.majorcourse.model.MajorCourseType;
import com.aiyige.utils.ListUtil;
import com.aiyige.utils.MaxHeightRecyclerView;
import com.aiyige.utils.widget.BaseBottomDialogFragment;
import com.chad.library.adapter.base.BaseQuickAdapter;

import java.util.LinkedList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

public class MajorCourseTypeDialog extends BaseBottomDialogFragment {

    @BindView(R.id.mainRv)
    MaxHeightRecyclerView mainRv;
    @BindView(R.id.cancelBtn)
    TextView cancelBtn;
    Unbinder unbinder;
    List<MajorCourseType> majorCourseTypeList = new LinkedList<>();
    Listener listener = null;
    MajorCourseTypeAdapter majorCourseTypeAdapter = null;


    public static MajorCourseTypeDialog newInstance() {
        return new MajorCourseTypeDialog();
    }

    public void setData(List<MajorCourseType> data) {
        if (!ListUtil.isEmpty(data)) {
            majorCourseTypeList = data;
            if (majorCourseTypeAdapter != null) {
                majorCourseTypeAdapter.setNewData(majorCourseTypeList);
            }
        }
    }

    public void setListener(Listener listener) {
        this.listener = listener;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.major_course_type_dialog, container, false);
        unbinder = ButterKnife.bind(this, view);
        mainRv.setLayoutManager(new LinearLayoutManager(view.getContext()));
        majorCourseTypeAdapter = new MajorCourseTypeAdapter();
        majorCourseTypeAdapter.setNewData(majorCourseTypeList);
        majorCourseTypeAdapter.bindToRecyclerView(mainRv);
        majorCourseTypeAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                for (MajorCourseType item : majorCourseTypeAdapter.getData()) {
                    item.setSelected(false);
                }
                MajorCourseType currentItem = majorCourseTypeAdapter.getItem(position);
                currentItem.setSelected(true);
                if (listener != null) {
                    listener.onClick(currentItem);
                }
                majorCourseTypeAdapter.notifyDataSetChanged();
                dismiss();
            }
        });
        return view;
    }

    @Override
    public void onCancel(DialogInterface dialog) {
        if (listener != null) {
            listener.onCancel();
        }
        super.onCancel(dialog);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        majorCourseTypeAdapter = null;
        unbinder.unbind();
    }

    @OnClick({R.id.cancelBtn})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.cancelBtn:
                if (listener != null) {
                    listener.onCancel();
                }
                dismiss();
                break;
        }
    }

    public interface Listener {
        void onClick(MajorCourseType type);

        void onCancel();
    }
}
